// work/pages/clockIn_record/clockIn_record.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPicker: false,
    value: '',
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    list: [],
    num: 0,
    year: '',
    mouth: '',
    day: '',
    listNum: 0,
    formatter(type, val) {
      if (type === 'year') {
        return val + '年';
      }
      if (type === 'month') {
        return val + '月';
      }
      if (type === 'day') {
        return val + '日';
      }
      return val;
    },
    activeNames: 1
  },
  onChange(e) {
    this.setData({
      activeNames: e.detail
    })
  },
  //判断时间遮罩标题
  timeData(e) {
    this.setData({
      showPicker: true,
      currentDate: new Date().getTime()
    })
  },
  // 选择时间
  resTime(e) {
    var time = new Date(e.detail),
      formartTime = this.formatDate(time);
    this.setData({
      value: formartTime,
      currentDate: formartTime,
      showPicker: false
    })
    this.getData()
  },
  formatDate(date) {
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  // 取消选择时间 
  selectCancel() {
    this.setData({
      showPicker: false
    })
  },

  getData() {
    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + 'main/listMemberMonth',
      that = this,
      listNum = 0;
    app.wxRequest(url, {
      memberId: app.globalData.memberId,
      dateTime: that.data.currentDate,
    }, res => {
      if (res.flag) {
        console.log(res);
        res.data.list = res.data.list.sort((a, b) => {
          return b.dailySignInId - a.dailySignInId
        })
        res.data.lastList = res.data.lastList.sort((a, b) => {
          return b.dailySignInId - a.dailySignInId
        })
        res.data.listOut = res.data.listOut.sort((a, b) => {
          return b.dailySignInId - a.dailySignInId
        })
        console.log(res);



        res.data.lastList = res.data.lastList.filter(item => {
          return item.signType == 1 && item.signTime.split(' ')[0] == that.data.currentDate
        })
        res.data.list = res.data.list.filter(item => {
          return item.signType == 0 && item.signTime.split(' ')[0] == that.data.currentDate
        })
        res.data.listOut = res.data.listOut.filter(item => {
          return item.signTime.split(' ')[0] == that.data.currentDate
        })

        listNum = res.data.lastList.length + res.data.list.length + res.data.listOut.length
        that.setData({
          list: res.data,
          listNum
        })
        console.log(this.data.list);
        console.log(listNum);
        wx.hideLoading()
      }
    })
  },

  //处理时间格式
  formatDate(date) {
    var year = '',
      mouth = '',
      day = '';
    year = date.getFullYear();
    mouth = this.add0(date.getMonth() + 1);
    day = this.add0(date.getDate());
    this.setData({
      year,
      mouth,
      day
    })

    return year + '-' + mouth + '-' + day
  },
  add0(num) {
    return num < 10 ? '0' + num : num
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      currentDate: this.formatDate(new Date()),
      value: this.formatDate(new Date()),
    })

    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})